package com.JAVA程序员面试宝典;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

/**
 * 输出数组中所有数字不同的组合，长度不限
 * 
 * @author qiuxiaochen
 */
public class 数组全排列 {

	private Set<String> strlist = new TreeSet<>();

	public static void main(String[] args) {
		数组全排列 t1 = new 数组全排列();
		int nums[] = { 1, 2, 3, 4};
		t1.AllArrange(nums);
		t1.lookResult();
	}

	public void AllArrange(int[] nums) {
		int i = 0;
		while (i < nums.length) {
			int zhong = nums[0];
			nums[0] = nums[i];
			nums[i] = zhong;
			ArrayList<String> arrayList = new ArrayList<>();
			arrayList.add(String.valueOf(nums[0]));
			for (int j = 1; j < nums.length; j++) {
				ArrayList<String> cache=new ArrayList<>();
				for (String content : arrayList) {
					cache.add(content + String.valueOf(nums[j]));
				}
				arrayList.addAll(cache);
			}
			strlist.addAll(arrayList);
			i++;
		}
	}

	// 打印出结果
	public void lookResult() {
		for (String str : strlist) {
			System.out.print(str + " ");
		}
	}

}
